﻿
using KrasDD.Dtos;
using KrasDD.Extensions;
using KrasDD.Models;
using KrasDD.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using System.Data;
using System.Data.Common;

namespace KrasDD.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class UserController(IFreeSql fsql) : ControllerBase  //,BaseRepository<USER>
    {
        [HttpGet]
        public List<USER> TestAccessPermission()
        {
            //var dddddw = fsql.Select<USER>().WhereUserHasACCESS().DisableGlobalFilter().ToSql();
            //Console.WriteLine("-------------------------------------");
            return fsql.Select<USER>().Where(l => l.HasACCESS2(l.PERMISSION_ID, l.CREATED_BY_ID, l.OWNED_BY_ID, l.TEAM_ID, true, false, false)).ToList();
        }
        [HttpGet]
        public dynamic GetUserTest()
        {
            var type = "user";
            var id = "30B991F927274FA3829655F50C99472E";
            var tableName = DBHelper.GetTableName(type, out var dbTableInfo);
            var sql = $"select * from  {tableName} where  id = @id ";
            var pams = new SqlParameter[] { new SqlParameter("@id", id) };
            var results = new List<CrudResonse>();
            // 1. 基本用法
            fsql.Ado.ExecuteReader((FreeSql.Internal.Model.FetchCallbackArgs<DbDataReader> reader) =>
            {
                DbDataReader? dataReader = reader.Object;

                var obj = dataReader.GetDictionary();

                results.Add(new CrudResonse() { id = id, type = type, data = obj });
            }, CommandType.Text, sql, pams);
            return ApiResponse<List<CrudResonse>>.Ok(results);
        }
    }
}
